﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

using Joyintech.Common;
using System.IO;
using System.Text;

namespace Joyintech.Admin.inpiao365
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            //创建日志目录
            if(!Directory.Exists(ConfigHelper.GetConfigString("LoggingDebugFilePath")))
            {
                Directory.CreateDirectory(ConfigHelper.GetConfigString("LoggingDebugFilePath"));
            }
            
            //读取系统界面配置相关
            if (systemUser_BLL.GetInstance().GetObject("Interface") == null)
            {
                systemUser_BLL.GetInstance().StoreObject("Interface", new systemInterface_BLL().GetModel(1));
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 系统操作日志
        /// </summary>
        /// <param name="content">日志内容</param>
        /// <param name="LogType">日志类别</param>
        /// <param name="UserTrueName">操作人员姓名</param>
        /// <param name="UserId">操作人员Id</param>
        public static void WriteLog(string content, string LogType, string UserTrueName, string UserName)
        {
            systemLog model = new systemLog();

            model.IP = DNTRequest.GetIP();
            model.LogRemark = content;
            model.LogType = LogType;
            model.UserDateTime = DateTime.Now;
            model.UserId = UserName;
            model.UserTrueName = UserTrueName;

            new systemLog_BLL().Add(model);
        }

        /// <summary>
        /// 返回当前系统使用的内容编辑器
        /// </summary>
        /// <param name="txtContent">内容框</param>
        /// <returns></returns>
        public static string GetTextEditor(string txtContent)
        {
            StringBuilder EditorHtml = new StringBuilder();

            string TextEditor = ConfigHelper.GetConfigString("TextEditor");

            if (TextEditor == "fckeditor")
            {
                EditorHtml.AppendLine("<script type=\"text/javascript\" src=\"/module/TextEditor/fckeditor/fckeditor.js\"></script>");

                EditorHtml.AppendLine("<script type=\"text/javascript\"> ");
                EditorHtml.AppendLine("<!--");
                EditorHtml.AppendLine("var oFCKeditor_Content = new FCKeditor('" + txtContent + "') ;");
                EditorHtml.AppendLine("oFCKeditor_Content.BasePath = '/module/TextEditor/fckeditor/' ;");
                EditorHtml.AppendLine("oFCKeditor_Content.Config['CustomConfigurationsPath'] = '/module/TextEditor/fckeditor/my.config.js';");
                EditorHtml.AppendLine("oFCKeditor_Content.ToolbarSet = 'MyToolbarSet' ;");
                EditorHtml.AppendLine("oFCKeditor_Content.Height = 400 ;");
                EditorHtml.AppendLine("oFCKeditor_Content.Width = '550px' ;");
                EditorHtml.AppendLine("oFCKeditor_Content.ReplaceTextarea();");
                EditorHtml.AppendLine("//-->");
                EditorHtml.AppendLine("</script>");
            }
            else if (TextEditor == "ckeditor")
            {
                EditorHtml.AppendLine("<script type=\"text/javascript\" src=\"/module/TextEditor/ckeditor/ckeditor.js\"></script>");

                EditorHtml.AppendLine("<script type=\"text/javascript\"> ");
                EditorHtml.AppendLine("<!--");
                EditorHtml.AppendLine("<!--");
                EditorHtml.AppendLine("CKEDITOR.replace('" + txtContent + "',");
                EditorHtml.AppendLine("{");
                EditorHtml.AppendLine("customConfig : '/module/TextEditor/ckeditor/my_config.js',");
                EditorHtml.AppendLine("height:280,");
                EditorHtml.AppendLine("filebrowserImageUploadUrl : '/module/TextEditor/ckeditor/upload.aspx?type=Image&publishmentSystemID=150',");
                EditorHtml.AppendLine("filebrowserFlashUploadUrl : '/module/TextEditor/ckeditor/upload.aspx?type=Flash&publishmentSystemID=150'");
                EditorHtml.AppendLine("});");
                EditorHtml.AppendLine("//-->");
                EditorHtml.AppendLine("</script>");
            }
            else if (TextEditor == "eWebEditor")
            {
                EditorHtml.AppendLine("<script>document.getElementById('" + txtContent + "').style.display = 'none;';</script>");

                EditorHtml.AppendLine("<script type=\"text/javascript\" src=\"/module/TextEditor/ckeditor/ckeditor.js\"></script>");

                EditorHtml.AppendLine("<IFRAME ID=\"eWebEditor_Content\" SRC=\"/module/TextEditor/eWebEditor/ewebeditor.htm?id=" + txtContent + "&style=coolblue&PublishmentSystemID=150\" FRAMEBORDER=\"0\" SCROLLING=\"no\" WIDTH=\"550\" HEIGHT=\"400\"></IFRAME>");
            }
            else
            {
                EditorHtml.AppendLine("<script>document.getElementById('" + txtContent + "').style.display = 'none;';</script>");

                EditorHtml.AppendLine("<iframe src=\"/module/TextEditor/xHtmlEditor/editor.htm?id=" + txtContent + "&ReadCookie=0&publishmentSystemID=150\" frameBorder=\"0\" marginHeight=\"0\" marginWidth=\"0\" scrolling=\"No\" width=\"621px\" height=\"457\"></iframe>");
            }

            return EditorHtml.ToString();
        }
    }
}